package com.coupon.service.impl;

import com.coupon.mapper.UserMapper;
import com.coupon.pojo.*;
import com.coupon.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author :wn
 * @program : coupon
 * @descript :
 * @create :2020-12-28 14:30
 */
@Service("userService")
public  class UserServiceImpl  implements UserService {
    @Resource
    UserMapper userMapper;
    @Override
    public Boolean register(User user) {
        //注册时，判断用户输入的手机号是否已经被注册过，
        User selectByPhoneUser = userMapper.selectByPhone(user.getPhone());
        int insert=0;
        //如果没有被注册，则继续注册流程
        if(selectByPhoneUser==null){
               insert=userMapper.addUser(user);
        }

       // 否则注册失败
     return insert>0;

    }

    @Override
    public User login(String phone) {
        User user = userMapper.selectByPhone(phone);
        return user;
    }

    @Override
    public PageObject findAll(Integer page, String name) {
        //页面做转换，转换为limit中的offset偏移量
       // Map dataMap=new HashMap();

        if(page==null&name==null){
            List<User> users = userMapper.selectAllNoArgs();
            PageObject<User> pageObject = new PageObject<>();
            pageObject.setDataList(users);
            return pageObject;
        }else {
            int count = userMapper.selectCount();
            List<User> users = userMapper.selectAll((page - 1) * 10, name);
            PageObject<User> pageObject = new PageObject<>();
            pageObject.setDataList(users);
            pageObject.setCount(count);
            return pageObject;
        }
    }



    @Override
    public int getCount() {
        int i = userMapper.selectCount();
        return i;
    }

    @Override
    public int getCpn(UserGet userGet) {
        int coupon = userMapper.getCoupon(userGet);
        return coupon;
    }

    @Override
    public PageObject selectAllUserGet(Integer page, String phone) {
        PageObject pageObject = new PageObject();
        if(phone==null){
            List<CouponList> couponLists = userMapper.selectAllUserGet();
            pageObject.setDataList(couponLists);
            return pageObject;
        }else {
            List<CouponList> userGets = userMapper.selectHadArg((page-1)*10,phone);
            int count = userMapper.selectAllUserGetCount();
            pageObject.setCount(count);
            pageObject.setDataList(userGets);
            return pageObject;
        }
    }

    @Override
    public int getUserGetCount() {
        int count = userMapper.selectUserGetCount();
        return count;
    }

    @Override
    public PageObject selectUsedList(String from,String  to) {
        List<ExchangeCoupon> exchangeCoupons = userMapper.selectUsedCoupon(from, to);
        PageObject<ExchangeCoupon> pageObject=new PageObject<>();
        pageObject.setDataList(exchangeCoupons);
        return pageObject;
    }

    @Override
    public PageObject notGetCoupon(String phone, String date) {
        List<Coupon> coupons = userMapper.selectNotGet(phone, date);
        PageObject<Coupon> couponPageObject=new PageObject<>();
        couponPageObject.setDataList(coupons);
        return couponPageObject;
    }

    @Override
    public PageObject hadGetCoupon(String phone, String date) {
        List<Coupon> coupons = userMapper.selectHadGet(phone, date);
        PageObject<Coupon> couponPageObject=new PageObject<>();
        couponPageObject.setDataList(coupons);
        return couponPageObject;
    }

    @Override
    public PageObject outTimeCoupon(String phone, String date) {
        List<Coupon> coupons = userMapper.selectOutTime(phone, date);
        PageObject<Coupon> couponPageObject=new PageObject<>();
        couponPageObject.setDataList(coupons);
        return couponPageObject;
    }

    @Override
    public int writeOff(Integer cpId, String phone) {
        int i = userMapper.writeOff(cpId, phone);
        return i;
    }

    @Override
    public PageObject selectUserGet(String phone) {
        List<UserGet> userGets = userMapper.UserGetCpnAll(phone);
        PageObject<UserGet> pageObject=new PageObject<>();
        pageObject.setDataList(userGets);
        return pageObject;
    }

    @Override
    public PageObject overExpireCpn(String phone) {
        List<UserGet> userGets = userMapper.overExpire(phone);
        PageObject<UserGet> pageObject=new PageObject<>();
        pageObject.setDataList(userGets);
        return pageObject;
    }

    @Override
    public PageObject selectExchangeList(Integer page, String from, String to) {
        List<ExchangeCoupon> exchangeCoupons = userMapper.selectChangeList((page - 1) * 10, from, to);
        PageObject pageObject=new PageObject();
        pageObject.setDataList(exchangeCoupons);
        return pageObject;
    }


}
